
from model.mdl import DEFAULT_TCM

from sortedcontainers import SortedSet

MAXIMUM_PERIODS_ONE_DAY = 5
WORK_DAYS = 5


class LocationMatrix(object):
    def __init__(self):
        self.matrix = [[DEFAULT_TCM] * MAXIMUM_PERIODS_ONE_DAY] * WORK_DAYS
        self.matrix_index = SortedSet([(p, w) for p in range(1, MAXIMUM_PERIODS_ONE_DAY+1) 
                                       for w in range(1, WORK_DAYS+1)])   # (1,1), (1,2), (2,1)
        

    # TODO 作为hook自动被调用
    def clear_index(self, index):
        self.matrix_index.discard(index)


class SchedulingHandler(object):
    def __init__(self):
        self.loc2matrix = dict()
        self.tck2rule = dict()
        self.tck = [DEFAULT_TCM]
        self.scheded_course = set()
        # self.unscheded_course = set() 增量scheduling

    def Upsert(self, location, matrix):
        self.loc2matrix.update(location, matrix)

    def Delete(self, location):
        self.loc2matrix.pop(location)

    def FetckTCKs(self, ):
        # 1 fetch all tck by hook
        pass
        # 2 order tcks

    def scheduling(self):
        """
        TODO 增量scheduling
        """
        for location in self.loc2matrix:
            self._scheduling(location)


    def _scheduling(self, location):
        for _tck in self.tck:
            if not _tck.Get_Cm().Compatible_All_Locations() and not _tck.Get_Cm().In_Locations():
                continue
